import axios, { AxiosResponse } from "axios";
import { UserModel } from "../models/User";

class UserApis {
    // 密码登录
    loginByPassword(phone: string, password: string): Promise<UserModel> {
        const promise: Promise<UserModel> = new Promise((resolve, reject) => {
            axios.post('/api/auth/login', { phone, password })
                .then((result: AxiosResponse) => {
                    if (result.data.code === 200) {
                        resolve(result.data.data);
                    } else {
                        reject(result.data);
                    }
                })
                .catch((error) => {
                    reject(error);
                })
        });
        return promise;
    }

    // 验证码登录
    loginByCode(phone: string, code: string): Promise<UserModel> {
        const promise: Promise<UserModel> = new Promise((resolve, reject) => {
            axios.post('/api/auth/login-with-code', { phone, code })
                .then((result: AxiosResponse) => {
                    if (result.data.code === 200) {
                        resolve(result.data.data);
                    } else {
                        reject(result.data);
                    }
                })
                .catch((error) => {
                    reject(error);
                })
        });
        return promise;
    }

    // 发送验证码
    sendVerifyCode(phone: string, code_type: string = 'login'): Promise<string> {
        const promise: Promise<string> = new Promise((resolve, reject) => {
            axios.post('/api/auth/send-code', { phone, code_type })
                .then((result: AxiosResponse) => {
                    if (result.data.code === 200) {
                        // 返回验证码（仅开发环境）
                        resolve(result.data.data.code);
                    } else {
                        reject(result.data);
                    }
                })
                .catch((error) => {
                    reject(error);
                })
        });
        return promise;
    }

    queryUser(id: string): Promise<UserModel> {
        const promise: Promise<UserModel> = new Promise((resolve, reject) => {
            let data: UserModel = {
                id: id
            }
            axios.get(`/api/user/user`, { params: data })
                .then((reslut: AxiosResponse) => {
                    let obj: UserModel = reslut.data.data;
                    if (reslut.data.code === 200) {
                        resolve(obj);
                    } else {
                        reject(reslut.data);
                    }
                })
                .catch(() => {
                    reject(null);
                })
        });
        return promise;
    }

    checkUser(phone: string): Promise<boolean> {
        const promise: Promise<boolean> = new Promise((resolve, reject) => {
            let data: UserModel = {
                phone: phone
            }
            axios.get(`/api/user/check`, { params: data })
                .then((reslut: AxiosResponse) => {
                    if (reslut.data.code === 101000) {
                        resolve(false);
                    } else {
                        resolve(true);
                    }
                })
                .catch(() => {
                    reject(null);
                })
        });
        return promise;
    }

    userRegister(user: UserModel): Promise<UserModel> {
        const promise: Promise<UserModel> = new Promise((resolve, reject) => {
            axios.post('/api/user/user', user, {
                headers: {
                    "Content-Type": 'application/x-www-form-urlencoded'
                }
            })
                .then((reslut: AxiosResponse) => {
                    const code: number = reslut.data.code;
                    if (code == 200) {
                        let user: UserModel = reslut.data.data;
                        resolve(user);
                    } else {
                        reject(reslut.data);
                    }
                })
                .catch((error) => {
                    reject(error);
                })
        });
        return promise;
    }
}

export const userApis: UserApis = new UserApis();